<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends MY_Controller {

	/**
	 * Member controller 
	 */
    public function __construct(){
        parent::__construct();
        $this->load->model('member_model');
        $this->load->library('flexigrid');
    }
	public function index()
	{
        //echo "Welcome to Sobic Administrator";
        //$this->view('list');ja
        //echo "Ajax";
        $valid_fields = array('memberId','username','firstName','lastName','departmentId','email');
		$this->flexigrid->validate_post('memberId','asc',$valid_fields);
        $records=$this->member_model->get_members();
        
        $this->output->set_header($this->config->item('json_header'));
		
		/*
		 * Json build WITH json_encode. If you do not have this function please read
		 * http://flexigrid.eyeviewdesign.com/index.php/flexigrid/example#s3 to know how to use the alternative
		 */
		$record_items = array();
        //debug($records['records']->result());
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->memberId,
            $row->memberId,
			$row->username,
			$row->firstName,
			$row->lastName,
			$row->departmentId,
			$row->email
			);
		}
         //debug($record_items);
        //Print please
		$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
        
       // debug($record_items);  
	}
    
    //Function for custom search form
	function search()
	{
		
		
		$_POST ['qtype'] = "username";
		$_POST ['query'] = $this->input->post('nsearch');
		
		$valid_fields = array('memberId','username','firstName','lastName','departmentId','email');
		$this->flexigrid->validate_post('memberId','asc',$valid_fields);
		
		$records=$this->member_model->get_members();
        //bug($records);
		
		$record_items = array();
        //debug($records['records']->result());
		foreach ($records['records']->result() as $row)
		{
			$record_items[] = array($row->memberId,
            $row->memberId,
			$row->username,
			$row->firstName,
			$row->lastName,
			$row->departmentId,
			$row->email
			);
		}
		//Print please
		$this->output->set_output($this->flexigrid->json_build($records['record_count'],$record_items));
	}
	
 }
    